# activecarddialog.py
# this is a dialog to allow the user to specify which cards from the vocab to be active

import sys
from PyQt4 import QtGui, QtCore

class ActiveCardDialog(QtGui.QDialog):
    def __init__(self, vocab, parent=None):
        QtGui.QDialog.__init__(self, parent)
        
        self.vocab = vocab
        
        self.setWindowTitle('Select Active Cards')
        self.layout = QtGui.QGridLayout(self)
        chapterValidator = QtGui.QIntValidator(0, 300, self)
        difficultyValidator = QtGui.QDoubleValidator(-500, 500, 2, self)
        self.chLoEdit = QtGui.QLineEdit()
        self.chLoEdit.setValidator(chapterValidator)
        self.chLoEdit.setFixedWidth(40)
        self.chHiEdit = QtGui.QLineEdit()
        self.chHiEdit.setValidator(chapterValidator)
        self.chHiEdit.setFixedWidth(40)
        self.difLoEdit = QtGui.QLineEdit()
        self.difLoEdit.setValidator(difficultyValidator)
        self.difLoEdit.setFixedWidth(40)
        self.difHiEdit = QtGui.QLineEdit()
        self.difHiEdit.setValidator(difficultyValidator)
        self.difHiEdit.setFixedWidth(40)
        self.englishSearch = QtGui.QLineEdit()
        self.foreignSearch = QtGui.QLineEdit()
        
        self.layout.addWidget(QtGui.QLabel('Chapters:', self), 0, 0)
        self.layout.addWidget(QtGui.QLabel('from', self), 0, 1)
        self.layout.addWidget(self.chLoEdit, 0, 2)
        self.layout.addWidget(QtGui.QLabel('to', self), 0, 3)
        self.layout.addWidget(self.chHiEdit, 0, 4)
        self.layout.addWidget(QtGui.QLabel('Difficulty:', self), 1, 0)
        self.layout.addWidget(QtGui.QLabel('from', self), 1, 1)
        self.layout.addWidget(self.difLoEdit, 1, 2)
        self.layout.addWidget(QtGui.QLabel('to', self), 1, 3)
        self.layout.addWidget(self.difHiEdit, 1, 4)
        self.layout.addWidget(QtGui.QLabel('English:', self), 2, 0)
        self.layout.addWidget(QtGui.QLabel('contains', self), 2, 1)
        self.layout.addWidget(self.englishSearch, 2, 2, 1, 3)
        self.layout.addWidget(QtGui.QLabel('Foreign:', self), 3, 0)
        self.layout.addWidget(QtGui.QLabel('contains', self), 3, 1)
        self.layout.addWidget(self.foreignSearch, 3, 2, 1, 3)
        
        self.checkBoxes = {}
        columns = 2
        column = 0
        row = 4
        width = 2
        for wordtype in self.vocab.wordtypes:
            self.checkBoxes[wordtype] = QtGui.QCheckBox(wordtype, self)
            self.checkBoxes[wordtype].setChecked(True)
            self.layout.addWidget(self.checkBoxes[wordtype], row, column * width, 1, width + column)
            column += 1
            if column >= columns:
                column = 0
                row += 1
        
        self.okButton = QtGui.QPushButton('OK', self)
        self.cancelButton = QtGui.QPushButton('Cancel', self)
        row = self.layout.rowCount()
        self.layout.addWidget(self.okButton, row, 1, 1, 2)
        self.layout.addWidget(self.cancelButton, row, 3, 1, 2)
        
        self.connect(self.okButton, QtCore.SIGNAL('clicked()'), self.ok)
        self.connect(self.cancelButton, QtCore.SIGNAL('clicked()'), self.cancel)
    
    def ok(self):
    
        numbers = {}
        words = {}
        wordtypes = []
        
        # if neither the hi nor lo entred we ignore that field, but if only one of the two entred we put something sensible in the other.
        if not (str(self.chLoEdit.text()) == '' and str(self.chHiEdit.text()) == ''):
            if str(self.chLoEdit.text()) == '':
                chLo = 0
            else:
                chLo = int(self.chLoEdit.text())
            if str(self.chHiEdit.text()) == '':
                chHi = 300
            else:
                chHi = int(self.chHiEdit.text())
            numbers['chapter'] = (chLo, chHi)
        if not (str(self.difLoEdit.text()) == '' and str(self.difHiEdit.text()) == ''):
            if str(self.difLoEdit.text()) == '':
                difLo = -500
            else:
                difLo = float(self.difLoEdit.text())
            if str(self.difHiEdit.text()) == '':
                difHi = 500
            else:
                difHi = float(self.difHiEdit.text())
            numbers['difficulty'] = (difLo, difHi)
            
        if not self.englishSearch.text() == '':
            words['english'] = unicode(self.englishSearch.text())
        if not self.foreignSearch.text() == '':
            words['foreign'] = unicode(self.foreignSearch.text())
            
        for wordtype in self.vocab.wordtypes:
            if self.checkBoxes[wordtype].isChecked():
                wordtypes.append(wordtype)
                
        self.vocab.activeCardFilter = CardFilter(numbers, words, wordtypes)
        self.vocab.setActiveCards()
        self.hide()
    
    def cancel(self):
        self.hide()

class CardFilter:
    
    def __init__(self, numbers, words, wordtypes, allCards = False):
        self.numbers = numbers
        self.words = words
        self.wordtypes = wordtypes
        self.allCards = allCards
    
    def filterCard(self, card):
        if self.allCards:
            print 'x'
            return True
        for fieldName in self.numbers:
            (min, max) = self.numbers[fieldName]
            if float(card.values[fieldName]) < min or float(card.values[fieldName]) > max:
                return False
        for fieldName in self.words:
            if not self.words[fieldName] in card.values[fieldName]:
                return False
        if card.wordType.name in self.wordtypes:
            return True
        else:
            return False
